package com.spit.fammo.adapter

import android.widget.Filter
import android.widget.Filterable
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.spit.fammo.R
import com.spit.fammo.bean.db.StockTakeListBean
import com.spit.fammo.ext.setAdapterAnimation
import com.spit.fammo.util.SettingUtil

/**
 * @Author nike
 * @Date 2023/7/7 17:05
 * @Description
 */
class StockTakeListAdapter(data: ArrayList<StockTakeListBean>) :
    BaseQuickAdapter<StockTakeListBean, BaseViewHolder>(R.layout.i_stock_tack, data), Filterable {

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(viewHolder: BaseViewHolder, bean: StockTakeListBean) {
        mFilterList[viewHolder.layoutPosition].run {
            viewHolder.setText(R.id.tv_title,  stocktakeno + " | " + name)
            viewHolder.setText(R.id.tv_brand,  startDate)
            viewHolder.setText(R.id.tv_model,  endDate)
            viewHolder.setText(R.id.tv_category,  "$progress" + "/" + "$total")
            viewHolder.setText(R.id.tv_location,  lastUpdate)
            viewHolder.setText(R.id.tv_remark,  remarks)

            when(status){
                0 ->{
                    viewHolder.setBackgroundColor(R.id.tv_status, ContextCompat.getColor(context, R.color.red_ff0000))
                    viewHolder.setText(R.id.tv_status, context.getText(R.string.download_all))
                }

                1 ->{
                    viewHolder.setBackgroundColor(R.id.tv_status, ContextCompat.getColor(context, R.color.colorPrimary))
                    viewHolder.setText(R.id.tv_status, context.getText(R.string.download_success))
                }
                else ->{}
            }
        }
    }

    var mFilterList = ArrayList<StockTakeListBean>()

    fun appendList(list: List<StockTakeListBean>) {
        data = list as MutableList<StockTakeListBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<StockTakeListBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = data as ArrayList<StockTakeListBean>
                } else {
                    val filteredList: MutableList<StockTakeListBean> = ArrayList()
                    for (i in data.indices) {
                        val bean = data[i]
                        if (bean.stocktakeno?.contains(charString, ignoreCase = true) == true
                            || bean.name?.contains(charString, ignoreCase = true) == true
                        ) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<StockTakeListBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<StockTakeListBean>
                notifyDataSetChanged()
            }
        }
    }

    interface SearchCallback {
        fun onSearchResults(filteredData: ArrayList<StockTakeListBean>)
    }

    private var searchCallback: SearchCallback? = null

    fun setSearchCallback(callback: SearchCallback) {
        searchCallback = callback
    }

    override fun getItemCount(): Int {
        return mFilterList.size
    }

    override fun hashCode(): Int {
        return mFilterList.hashCode()
    }

}